

. .\Lib.Strings.ps1
. .\Lib.MimeType.ps1
. .\Lib.WebPage.ps1


#====================================================================
#
#		POWERSHELL DOCUMENTATION WEB APPLICATION
#
#====================================================================


#--------------------------------------------------------------------
Function Get-HtmlPSProviderList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	[string] $html = ""
	$html += "<div id=""helptopics_providerlist"">"
	$html += "<a href=""$( $url + ""/provider"" )""><b>Providers</b></a>"
	$html += "<table cellspacing=""0"" cellpadding=""3"" border=""1"">"
	[psobject] $Topics = Get-Help -Name * -Category "Provider"
	[psobject] $Topic = $null
	foreach( $Topic in $Topics )
	{
		$html += "<tr>"
		$html += "<td><a href=""$( $url + ""/provider/"" + $Topic.Name )"">$( $Topic.Name )</a></td>"
		$html += "<td>$( $Topic.Category )</td>"
		$html += "<td>$( $Topic.Synopsis )</td>"
		$html += "</tr>"
	}
	$html += "</table>"
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Get-HtmlPSAliasList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	[string] $html = ""
	$html += "<div id=""helptopics_aliaslist"">"
	$html += "<a href=""$( $url + ""/alias"" )""><b>Aliases</b></a>"
	$html += "<table cellspacing=""0"" cellpadding=""3"" border=""1"">"
	[psobject] $Topics = Get-Help -Name * -Category "Alias"
	[psobject] $Topic = $null
	foreach( $Topic in $Topics )
	{
		$html += "<tr>"
		$html += "<td><a href=""$( $url + ""/alias/"" + $Topic.Name )"">$( $Topic.Name )</a></td>"
		$html += "<td>$( $Topic.Category )</td>"
		$html += "<td>$( $Topic.Synopsis )</td>"
		$html += "</tr>"
	}
	$html += "</table>"
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Get-HtmlPSCmdletList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	[string] $html = ""
	$html += "<div id=""helptopics_cmdletlist"">"
	$html += "<a href=""$( $url + ""/cmdlet"" )""><b>Cmdlets</b></a>"
	$html += "<table cellspacing=""0"" cellpadding=""3"" border=""1"">"
	[psobject] $Topics = Get-Help -Name * -Category "Cmdlet"
	[psobject] $Topic = $null
	foreach( $Topic in $Topics )
	{
		if( $Topic.Category -eq "Cmdlet" )
		{
			$html += "<tr>"
			$html += "<td><a href=""$( $url + ""/cmdlet/"" + $Topic.Name )"">$( $Topic.Name )</a></td>"
			$html += "<td>$( $Topic.Category )</td>"
			$html += "<td>$( $Topic.Synopsis )</td>"
			$html += "</tr>"
		}
	}
	$html += "</table>"
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Get-HtmlPSHelpFileList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	[string] $html = ""
	$html += "<div id=""helptopics_helpfilelist"">"
	$html += "<a href=""$( $url + ""/helpfile"" )""><b>Help Files</b></a>"
	$html += "<table cellspacing=""0"" cellpadding=""3"" border=""1"">"
	[psobject] $Topics = Get-Help -Name * -Category "HelpFile"
	[psobject] $Topic = $null
	foreach( $Topic in $Topics )
	{
		$html += "<tr>"
		$html += "<td><a href=""$( $url + ""/helpfile/"" + $Topic.Name )"">$( $Topic.Name )</a></td>"
		$html += "<td>$( $Topic.Category )</td>"
		$html += "<td>$( $Topic.Synopsis )</td>"
		$html += "</tr>"
	}
	$html += "</table>"
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Get-HtmlTopic
{
	param
	(
		  [psobject]	$WebRequest
		, [string]		$Name
	)
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	[string] $html = ""
	$html += "<div id=""helptopic"">"
	$html += "<b><em>THIS FUNCTION IS BROKEN AND NEEDS WORK.</b></em><br />"
	[string] $Topic = Get-Help -Name $Name -Full
	$html += $Topic
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Process-Web-GET
{
	param
	(
		  [psobject] $WebRequest
	)

	[psobject] $WebResponse = New-WebResponse
	$WebResponse.WebRequest		= $WebRequest
	$WebResponse.Resource		= $null
	$WebResponse.ResponseCode	= "501 Not Implemented"
	
Write-Host "GET -> $( $WebRequest.ResourceName )"
	[string] $html = ""

	# Build the web page.
	[string] $html = Get-HtmlHeader $WebRequest
	$html += "<h3>$( $WebRequest.WebServer.ApplicationName )</h3>"
	$html += Get-HtmlNavigationMenu $WebRequest
	$html += "<br />"

	if( $WebRequest.ResourceName -eq "" )
	{
		$html += Get-HtmlPSProviderList $WebRequest
		$html += "<br />"
		$html += Get-HtmlPSCmdletList $WebRequest
		$html += "<br />"
		$html += Get-HtmlPSHelpFileList $WebRequest
		$html += "<br />"
		$html += Get-HtmlPSAliasList $WebRequest
		$html += "<br />"
	}
	else
	{
		[string[]] $PathNames = $WebRequest.ResourceName.Split( "\" )
		[string] $Category = ""
		[string] $Name = ""
		if( $PathNames.Length -gt 0 )
		{
			$Category = $PathNames[0]
			if( $PathNames.Length -gt 1 )
			{
				$Name = $PathNames[1]
				$html += Get-HtmlTopic $WebRequest $Name
			}
			else
			{
				if( $Category -eq "provider" )
					{ $html += Get-HtmlPSProviderList $WebRequest }
				elseif( $Category -eq "cmdlet" )
					{ $html += Get-HtmlPSCmdletList $WebRequest }
				elseif( $Category -eq "helpfile" )
					{ $html += Get-HtmlPSHelpFileList $WebRequest }
				elseif( $Category -eq "alias" )
					{ $html += Get-HtmlPSAliasList $WebRequest }
			}
		}
	}
	$html += Get-HtmlFooter $WebRequest

	# Construct the response.
	$WebResponse.Resource = New-Resource
	$WebResponse.Resource.Root = ""
	$WebResponse.Resource.Path = $WebRequest.ResourceName
	$WebResponse.Resource.Name = ""
	$WebResponse.Resource.Mime = "text/html"
	$WebResponse.Resource.Data = AsciiString2Bytes( $html )
	$WebResponse.Resource.Size = $WebResponse.Resource.Data.Length
	$WebResponse.ResponseCode = "200 OK"

	# Send the response header.
Write-Host $WebResponse.ResponseCode
	[bool] $b = Send-WebResponseHeader $WebResponse
	if( $b -eq $false ) { return $false }

	# Send the resource content.
	if( $WebResponse.Resource -ne $null )
	{
		#Write-Debug -Message $Resource.Data
	    $cbSent = $Socket.Send( $WebResponse.Resource.Data )
	    if ( $cbSent -eq -1 ) { return $false }
    }
	return $true

}

